var urlData = require('../../../../../data/url.js');

Page({

  /**
   * 页面的初始数据
   */
  data: {
    userId:'',
    courseId:'',
    stu_id:'',
    itemArray: {},
    searchinput: '',
    leave_type:'',
    items: [
      { name: '1', value: '病假' },
      { name: '2', value: '事假' },
      { name: '3', value: '公假' },
      { name: '4', value: '其他' },
    ],
    onloadUrl: urlData.urlList.teachingManage.leaveManagestudent.url,
    tapNameUrl:urlData.urlList.teachingManage.leaveManagestudentPost.url,
    nowDate: '',
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (e) {
    var that = this;
    var dateObj = new Date();
    var nowYear = dateObj.getFullYear(); 
    var nowMonth = dateObj.getMonth()+1;
    var nowDay = dateObj.getDate();
    var nowDate = nowYear + '-' + nowMonth + '-' + nowDay;
    console.log(nowDate)
    this.setData({
      nowDate : nowDate
    });
    wx.getStorage({
      key:'loginConf',
      success:function(res){
        that.setData({
          userId : res.data.userId
        });
      }
    });
    wx.getStorage({
      key:'submitCourse',
      success:function(res){
        that.setData({
          courseId : res.data.course_id
        });
        wx.request({
          url: that.data.onloadUrl,
          method: "get",
          data: {
            user_id: that.data.userId,
            course_id: that.data.courseId
          },
          header: {
            'content-type': 'application/x-www-form-urlencoded'
          },
          success: function (res) {
            if (res.data.code == 0) {
              that.setData({
                stu_id : res.data.list.stu_id
              });
              that.listData(res);
            }
          },
          fail: function (res) {
            console.log('刷list数据失败');
          }
        });
      }
    });
  },
  bindDateChange: function(e) {
    this.setData({
      nowDate: e.detail.value
    })
  },
  radioChange: function(e) {
    this.setData({
      leave_type: e.detail.value
    })
  },
  leaveInput: function (e) {//获取请假原因的输入框的value值
    this.setData({
      leave_reason: e.detail.value
    })
  },
  tapName: function (e) {//提交按钮
    if (this.data.leave_reason == undefined || this.data.leave_reason == '') {//请假原因
      wx.showModal({
        title: '提交提示',
        content: '请假原因不能为空！',
        success: function (res) {
          if (res.confirm) {
            console.log('用户点击确定')
          } else if (res.cancel) {
            console.log('用户点击取消')
          }
        }
      })
    } else {
      this.setData({
        searchinput: '',
      });
      this.sendLeaveData();
    }
  },
  /*
    刷list数据
  */
  listData: function (res) {
    this.setData({
      itemArray: res.data.list
    });
  },
  /*
    发送提交数据
  */
  sendLeaveData: function (e) {
    wx.request({
      url: this.data.tapNameUrl,
      'content-type': 'application/json',
      method: "get",
      data: {
        stu_id:this.data.stu_id,
        user_id: this.data.userId,
        course_id: this.data.courseId,
        leave_date: this.data.nowDate,//请假日期
        leave_type: this.data.leave_type,//请假类型
        leave_reason: this.data.leave_reason//请假原因
      },
      header: { 'content-type': 'application/x-www-form-urlencoded' },
      success: function (res) {
        if (res.data.code == 0) {
          wx.showModal({
            title: '提示',
            content: '请假提交成功!',
            success: function(res) {
              wx.navigateBack({
                delta: 1
              })
            }
          })
        }else if(res.data.code == 1){
          wx.showModal({
            title: '提示',
            content: '请假日期与上课日期不符！'
          })
        }
        else if(res.data.code == 2){
          wx.showModal({
            title: '提示',
            content: '这节课您已经请过假了！'
          })
        }
      },
      fail: function (res) {
        console.log('提交失败');
      }
    });
  }

});